Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

235
Visualizações
Make query with "where" filter in Firestore

I want to filter and get specific items based on the categoryID, where i use the "where" method and the "==" operator.

I have this Firestore collection called "task", where the documents are divided by userID and each document for a user contains an array of different tasks. The structur is seen here: Enter image description here

This is how i add the array tasks for an user in the task collection:

const addToFirebase = async() => {
    if(dataFetch) {
      await setDoc(doc(db, "task", `${firebase.auth().currentUser.uid}`), {
        tasks: tasks
      }, {merge: true});
    }
  }

And this is how i have tried to make my query that is not working:

 const getFilteredTasks = async() => {
    const collectionRef = collection(db, "task", `${firebase.auth().currentUser.uid}`, "tasks");
    const q = query(collectionRef, where("categoryID", "==", item.id));

    console.log('outside snapshot')
    console.log(item.id)

    const querySnapshot = await getDocs(q);
    querySnapshot.forEach((doc) => {
      console.log(doc.data());
      console.log('inside snapshot')
    });
  }

When the above function is called, it logs the "outside snapshot" and the correct item.id, which is the categoryID for each task, but it does not log the doc.data() and the "inside snapshot".

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You've set up your query as though each task will be a separate document in a collection, but based on the screenshot and the way you're setting the data, you just have a single document per user. That document contains all the individual tasks. So to access that you would get the document and use client side code to use the parts of the data you care about:

const getFilteredTasks = async () => {
  const docRef = doc(db, 'task', `${firebase.auth().currentUser.uid}`);
  const snapshot = await getDoc(docRef);
  const data = snapshot.data();
  if (!data) {
    return [];
  } else {
    return data.tasks.filter(task => task.categoryID === item.id);
  } 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda